package hz.mobileproject.pda.ui.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;
import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.request.NoneTalkPush;
import hz.mobileproject.pda.entity.response.NoneTalkListBean;
import hz.mobileproject.pda.presenter.PNoTalk;
import hz.mobileproject.pda.ui.adapter.NoneTalkListAdapter;

/**
 * Created by zmybi on 2017/8/23.
 */

public class NoneTalkPersonActivity extends XActivity<PNoTalk> {

    private static final String TAG = "NoneTalkPersonActivity";
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mSmartRefreshLayout;


    private NoneTalkListBean noneTalkListBean;
    private List<NoneTalkListBean> noneTalkListBeans;
    private NoneTalkListAdapter noneTalkListAdapter;

    private int page = 1;
    private String mCusnumber;
    private String mQueryTime;
    private String mQueryDept;
    private String mPolice_id;
    private String mPoliceCode;
    private String mSelectQueryTime;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);


    }

    @Override
    public void onWidgetClick(View view) {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_nontalk);
        mSmartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smartLayout);
        SPUtils spUtils = SPUtils.getInstance("app_login");

        mCusnumber = spUtils.getString("cusnumber");
        mQueryTime = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM"));
//        mQueryTime = TimeUtils.getNowString();
        mQueryDept = spUtils.getString("department_id");
        final String queryPage = page + "";
        mPolice_id = spUtils.getString("police_id");
        mPoliceCode = spUtils.getString("police_code");

        ImageView bt_select_time = (ImageView) findViewById(R.id.bt_select_time);
        bt_select_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickDialog dialog = new DatePickDialog(NoneTalkPersonActivity.this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //回显当前选择时间
                if(!StringUtils.isEmpty(mSelectQueryTime)){
                    try {
                        dialog.setStartDate(new SimpleDateFormat("yyyy-MM").parse(mSelectQueryTime));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        LogUtils.e(date);
                        page = 1;
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                        mSelectQueryTime = sdf.format(date);
                        LogUtils.e(mQueryTime);    //1974-01
                        mQueryTime = mSelectQueryTime;
                        //点击选择时间后 重新请求网络
                        NoneTalkPush selectTalkPush = new NoneTalkPush(mCusnumber, mSelectQueryTime, mQueryDept,queryPage, mPolice_id, mPoliceCode,false);
                        LogUtils.e("select",selectTalkPush);
                        Gson gson = new Gson();
                        String selectJson = gson.toJson(selectTalkPush);
                        getP().setData(selectJson,3);

                    }
                });
                dialog.show();


            }
        });
        //默认本月的数据
        NoneTalkPush noneTalkPush = new NoneTalkPush(mCusnumber, mQueryTime, mQueryDept,queryPage, mPolice_id, mPoliceCode,false);

        LogUtils.e(TAG,noneTalkPush.toString());
        Gson gson = new Gson();
        String json = gson.toJson(noneTalkPush);
        getP().setData(json,1);

        mSmartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                ToastUtils.showLong(page + "");
                NoneTalkPush noneTalkPush = new NoneTalkPush(mCusnumber, mQueryTime, mQueryDept,page + "", mPolice_id, mPoliceCode,false);
                Gson gson = new Gson();
                String json2 = gson.toJson(noneTalkPush);
                getP().setData(json2,2);

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_nonetalk_person;
    }

    @Override
    public PNoTalk newP() {
        return new PNoTalk();
    }


    public void setView(String json, int i) {
        switch (i) {
            case 1: {
                LogUtils.e(TAG,json);
                noneTalkListBeans = new Gson().fromJson(json,new TypeToken<List<NoneTalkListBean>>(){}.getType());
                if(noneTalkListBeans == null) {
                    ToastUtils.showLong("没有查询到数据");
                    return;
                }
                LogUtils.e(TAG,noneTalkListBeans.size());

                mRecyclerView.setLayoutManager(new LinearLayoutManager(NoneTalkPersonActivity.this));
                noneTalkListAdapter = new NoneTalkListAdapter(NoneTalkPersonActivity.this,noneTalkListBeans);
                mRecyclerView.setAdapter(noneTalkListAdapter);

            }
            break;
            case 2 : {
                mSmartRefreshLayout.finishLoadmore();
                LogUtils.e(TAG,json);
                //第二页的数据
                List<NoneTalkListBean> newnoneTalkListBeans = new Gson().fromJson(json,new TypeToken<List<NoneTalkListBean>>(){}.getType());
                if(newnoneTalkListBeans == null) {
                    ToastUtils.showLong("没有更多数据");
                    return;
                }
                LogUtils.e(TAG,noneTalkListBeans.size());

                noneTalkListAdapter.addData(newnoneTalkListBeans);


            }
            break;
            case 3 : {
                //先清空原始数据

                LogUtils.e(TAG,json);
                noneTalkListBeans =   new Gson().fromJson(json,new TypeToken<List<NoneTalkListBean>>(){}.getType());
//                LogUtils.e("select",noneTalkListBeans.size());

                noneTalkListAdapter.setData(noneTalkListBeans);

            }
            break;
        }
    }

}
